<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="流程编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入流程编号"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="流程名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入流程名"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="流程状态"
          clearable
          size="small"
          style="width: 200px">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:flow:add']">
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:flow:remove']">
          删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="initData"></right-toolbar>
    </el-row>

    <el-table v-loading="table.loading" :data="table.data" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="流程编号" align="center" prop="code" min-width="220"/>
      <el-table-column label="流程名" align="center" prop="name" width="200"/>
      <el-table-column label="对应的表" align="center" prop="mainTable" min-width="240"/>
      <el-table-column label="所属产业" align="center" prop="corp" width="120"/>
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="140">
        <template slot-scope="scope">
          <el-link
            type="primary"
            icon="el-icon-edit"
            :underline="false"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:flow:edit']">
            修改
          </el-link>
          <el-link
            type="danger"
            icon="el-icon-delete"
            :underline="false"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:flow:remove']">
            删除
          </el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="page.total > 0"
      :total="page.total"
      :page.sync="page.current"
      :limit.sync="page.size"
      @pagination="initData"/>

    <edit ref="edit" @success="handleQuery" :status-options="statusOptions"></edit>
  </div>
</template>

<script>
import Edit from "./components/edit";

import { flowTemplateList, deleteFlowTemplate, changeFlowTemplateStatus } from "@/api/system/flowTemplate";

export default {
  name: "FlowIndex",
  components: {
    Edit
  },
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        code: undefined,
        name: undefined,
        status: undefined
      },
      table: {
        data: [],
        loading: false,
      },
      page: {
        total: 0,
        current: 1,
        size: 10,
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.initData();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response;
      });
    },
    /** 查询角色列表 */
    initData() {
      this.table.loading = true;
      flowTemplateList({
        code: this.queryParams.code,
        name: this.queryParams.name,
        status: this.queryParams.status,
        page: true,
        current: this.page.current,
        size: this.page.size
      }).then(response => {
        this.table.data = response.records;
        this.page.total = response.total;
        this.table.loading = false;
      }).catch(() => {
        this.table.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.page.current = 1;
      this.initData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.edit.init(null, '新增流程模板');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.edit.init(row.id, '编辑流程模板');
    },
    // 角色状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要 "' + text + '" "' + row.name + '" 流程模板吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeFlowTemplateStatus(row.id, row.status);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const templateIds = row.id || this.ids;
      this.$confirm('是否确认删除流程模板?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return deleteFlowTemplate(templateIds);
      }).then(() => {
        this.initData();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
}
</script>

<style scoped>

</style>
